/**
 * 
 */
package com.chinasofti.project.service.order.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chinasofti.project.common.RandomUtils;
import com.chinasofti.project.dal.mapper.BzOrderDatailMapper;
import com.chinasofti.project.dal.mapper.BzOrderMapper;
import com.chinasofti.project.dal.mapper.BzUserMapper;
import com.chinasofti.project.dal.mapper.custom.BzBookProductMapperCustom;
import com.chinasofti.project.dal.mapper.custom.BzShippingAddressMapperCustom;
import com.chinasofti.project.dal.mapper.custom.BzShoppingCartMapperCustom;
import com.chinasofti.project.dal.model.BzOrder;
import com.chinasofti.project.dal.model.BzOrderDatail;
import com.chinasofti.project.dal.model.BzShippingAddress;
import com.chinasofti.project.dal.model.BzShoppingCartExample;
import com.chinasofti.project.dal.model.BzUserExample;
import com.chinasofti.project.dal.model.custom.BzProduct;
import com.chinasofti.project.service.order.OrderService;
import com.chinasofti.project.web.form.OrderForm;
import com.chinasofti.project.web.info.OrderResult;

/**
 * @author yanghuailong
 * 
 * @Date 2018年9月28日上午10:00:27
 */
@Service("orderService")
public class OrderServiceImpl implements OrderService {

	@Autowired
	private BzOrderMapper bzOrderMapper;

	@Autowired
	private BzOrderDatailMapper bzOrderDatailMapper;

	@Autowired
	private BzBookProductMapperCustom bzBookProductMapperCustom;

	@Autowired
	private BzShippingAddressMapperCustom bzShippingAddressMapperCustom;

	@Autowired
	private BzUserMapper bzUserMapper;

	@Autowired
	private BzShoppingCartMapperCustom bzShoppingCartMapperCustom;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.chinasofti.project.service.order.OrderService#createOrder(java.util.
	 * List, java.util.List, int)
	 */
	@Override
	@Transactional
	public OrderResult createOrder(List<Integer> productIdes, List<Integer> scProductNums, int userId) {
		// 生成订单
		BzOrder bzOrder = new BzOrder();
		bzOrder.setUserId(userId);
		String orderNumber = RandomUtils.randomString(12);
		bzOrder.setOrderNumber(orderNumber);
		bzOrderMapper.insertSelective(bzOrder);
		// 查询各种结果
		double orderShouldPayment = 0;
		List<BzShippingAddress> addressList = bzShippingAddressMapperCustom.selectByUserId(userId);
		List<BzProduct> bzproductes = new ArrayList<>();
		for (int i = 0; i < productIdes.size(); i++) {
			// 得到Result的数据
			int productId = productIdes.get(i);
			int scProductNum = scProductNums.get(i);
			BzProduct bp = bzBookProductMapperCustom.selectByProductId(productId);
			double totalPrice = scProductNum * bp.getProductPrice();
			bp.setScProductNum(scProductNum);
			bp.setTotalPrice(totalPrice);
			bzproductes.add(bp);
			orderShouldPayment += totalPrice;
			// 生成订单详情
			BzOrderDatail bzOrderDatail = new BzOrderDatail();
			bzOrderDatail.setOrderId(bzOrder.getOrderId());
			bzOrderDatail.setProductId(productId);
			bzOrderDatail.setOrderProductNum(scProductNum);
			BigDecimal orderProductPrice = new BigDecimal(bp.getProductPrice());
			bzOrderDatail.setOrderProductPrice(orderProductPrice);
			BigDecimal orderTotalPrice = new BigDecimal(totalPrice);
			bzOrderDatail.setOrderTotalPrice(orderTotalPrice);
			bzOrderDatailMapper.insert(bzOrderDatail);
			// 删除购物车中的数据
			BzShoppingCartExample example = new BzShoppingCartExample();
			example.createCriteria().andProductIdEqualTo(productId).andUserIdEqualTo(userId);
			bzShoppingCartMapperCustom.deleteByExample(example);
		}
		BigDecimal bigdecimal = new BigDecimal(orderShouldPayment);
		bzOrder.setOrderShouldPayment(bigdecimal);
		// 查询用户余额
		BzUserExample bzUserExample = new BzUserExample();
		bzUserExample.createCriteria().andUserIdEqualTo(userId);
		BigDecimal userBalance = bzUserMapper.selectByExample(bzUserExample).get(0).getUserBalance();
		// 生成返回Result
		OrderResult orderResult = new OrderResult();
		orderResult.setAddressList(addressList);
		orderResult.setProductList(bzproductes);
		orderResult.setUserBalance(userBalance);
		orderResult.setBzOrder(bzOrder);
		return orderResult;
	}

	@Override
	@Transactional
	public void updateOrder(OrderForm orderForm) {
		BzOrder bzOrder = new BzOrder();
		bzOrder.setOrderId(orderForm.getOrderId());
		bzOrder.setOrderState(orderForm.getOrderState());
		bzOrder.setOrderCurrAddress(orderForm.getOrderCurrAddress());
		bzOrder.setOrderShouldPayment(orderForm.getOrderShouldPayment());
		bzOrder.setOrderRealPayment(orderForm.getOrderRealPayment());
		bzOrder.setOrderPaymentTime(orderForm.getOrderPaymentTime());
		bzOrderMapper.updateByPrimaryKeySelective(bzOrder);
	}
}
